package com.fate.securecheckplatform.views;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fate.securecheckplatform.R;
import com.fate.securecheckplatform.adapters.ChooseForbiddenNameAdapter;
import com.fate.securecheckplatform.bean.TableForbiddenType;

import java.util.List;


public class ChooseForbiddenNameDialog extends Dialog {

    private ChooseForbiddenNameDialog(Context context) {
        super(context, R.style.Theme_AppCompat_Dialog);
    }

    public static class Builder {

        private View mLayout;

        private TextView mTvCancel;
        private RecyclerView mRv;
        private List<TableForbiddenType> mDatas;
        private ChooseForbiddenNameAdapter adapter;
        private View.OnClickListener mButtonClickListener;
        private ChooseForbiddenNameAdapter.OnItemClickListener itemClickListener;

        private ChooseForbiddenNameDialog mDialog;

        public Builder(Context context, List<TableForbiddenType> mDatas) {
            this.mDatas = mDatas;
            mDialog = new ChooseForbiddenNameDialog(context);
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //加载布局文件
            mLayout = inflater.inflate(R.layout.dialog_choose, null, false);
            //添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

            mRv = mLayout.findViewById(R.id.rv_choose);
            mTvCancel = mLayout.findViewById(R.id.tv_cacel);
            mRv.setHasFixedSize(true);
            adapter = new ChooseForbiddenNameAdapter(context, mDatas);
            mRv.setLayoutManager(new LinearLayoutManager(context));
            mRv.addItemDecoration(new RecyclerViewDivider(context));
            mRv.setAdapter(adapter);

        }


        /**
         * 设置按钮文字和监听
         */
        public Builder setCancleListener(View.OnClickListener listener) {
            mButtonClickListener = listener;
            return this;
        }


        public Builder setOnItemClickListener(ChooseForbiddenNameAdapter.OnItemClickListener listener) {
            itemClickListener = listener;
            return this;
        }

        public Builder setDatas(List<TableForbiddenType> datas) {
            mDatas = datas;
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
            return this;
        }

        public ChooseForbiddenNameDialog create() {
            mTvCancel.setOnClickListener(view -> {
                mDialog.dismiss();
                mButtonClickListener.onClick(view);
            });
            adapter.setOnItemClickListener(itemClickListener);
            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);                //用户可以点击后退键关闭 Dialog
            mDialog.setCanceledOnTouchOutside(false);   //用户不可以点击外部来关闭 Dialog
            return mDialog;
        }

        public void showNow() {
            if (mDialog != null) {
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                mDialog.show();
            }
        }
    }
}